from common.base import Base, get_driver
from selenium.webdriver.common.by import By


class AddressPage(Base):

    url = 'http://localhost:8080/ecshop/index.php'

    def add_address(self):
        '''
        self.click((By.XPATH, '//form[@action="user.php"][2]/table//select[2]/option[@value="24"]'))
        self.click((By.XPATH, '//form[@action="user.php"][2]/table//select[3]/option[@value="271"]'))
        self.click((By.XPATH, '//form[@action="user.php"][2]/table//select[4]/option[@value="2716"]'))
        '''

        # 下拉框选择收货地址
        self.click((By.XPATH, '//form[@action="user.php"][last()]/table//select[2]/option[contains(text(), "四川省")]'))
        self.click((By.XPATH, '//form[@action="user.php"][last()]/table//select[3]/option[contains(text(), "成都市")]'))
        self.click((By.XPATH, '//form[@action="user.php"][last()]/table//select[4]/option[contains(text(), "武侯区")]'))

        # 输入收货人信息
        self.send_keys((By.XPATH, '//form[@action="user.php"][last()]/table//tr[2]//input'), '1号')
        self.send_keys((By.XPATH, '//form[@action="user.php"][last()]/table//tr[3]//input'), '1小区')
        self.send_keys((By.XPATH, '//form[@action="user.php"][last()]/table//tr[4]//input'), '15545676545')
        self.click((By.XPATH, '//form[@action="user.php"][last()]/table//tr[last()]//input'))

    def get_name(self):

        return self.get_attribute((By.XPATH, '//form[@action="user.php"][1]/table//tr[2]//input'), 'value')

    def alter_address(self):

        # 修改下拉框地址
        # self.click((By.XPATH, '//table[@cellpadding="5"]//tr/td/select[last()]/option[@value="24"]'))
        self.click((By.XPATH, '//form[@action="user.php"][1]/table//select[2]/option[contains(text(), "四川省")]'))
        self.click((By.XPATH, '//form[@action="user.php"][1]/table//select[3]/option[contains(text(), "成都市")]'))
        self.click((By.XPATH, '//form[@action="user.php"][1]/table//select[4]/option[contains(text(), "青羊区")]'))

        # TODO 修改收货人信息
        # 删除输入框内收货人信息
        self.find_element((By.XPATH, '//form[@action="user.php"][1]/table//tr[2]//input')).clear()
        self.find_element((By.XPATH, '//form[@action="user.php"][1]/table//tr[3]//input')).clear()
        self.find_element((By.XPATH, '//form[@action="user.php"][1]/table//tr[4]//input')).clear()
        # 添加输入框内收货人信息
        self.send_keys((By.XPATH, '//form[@action="user.php"][1]/table//tr[2]//input'), '2号')
        self.send_keys((By.XPATH, '//form[@action="user.php"][1]/table//tr[3]//input'), '2小区')
        self.send_keys((By.XPATH, '//form[@action="user.php"][1]/table//tr[4]//input'), '17565453421')
        self.click((By.XPATH, '//form[@action="user.php"][1]/table//tr[last()]//input[1]'))

    def delete_address(self):

        # 删除收货地址
        self.click((By.XPATH, '//form[@action="user.php"][1]/table//tr[last()]//input[2]'))
        confirm = self.driver.switch_to.alert
        confirm.accept()

    # 配合其他模块添加收货地址
    def add_address1(self):

        # 下拉框选择收货地址
        self.click((By.XPATH, '//form[@action="user.php"][last()]/table//select[2]/option[contains(text(), "四川省")]'))
        self.click((By.XPATH, '//form[@action="user.php"][last()]/table//select[3]/option[contains(text(), "成都市")]'))
        self.click((By.XPATH, '//form[@action="user.php"][last()]/table//select[4]/option[contains(text(), "武侯区")]'))

        # 输入收货人信息
        self.send_keys((By.XPATH, '//form[@action="user.php"][last()]/table//tr[2]//input'), '第八组')
        self.send_keys((By.XPATH, '//form[@action="user.php"][last()]/table//tr[3]//input'), '源码时代')
        self.send_keys((By.XPATH, '//form[@action="user.php"][last()]/table//tr[4]//input'), '16654565678')
        self.click((By.XPATH, '//form[@action="user.php"][last()]/table//tr[last()]//input'))


if __name__ == '__main__':

    driver = get_driver()
    address = AddressPage(driver)
    address.get('http://localhost:8080/ecshop/user.php?act=address_list')
    address.send_keys((By.NAME, 'username'), 'root')
    address.send_keys((By.NAME, 'password'), '123456')
    address.click((By.NAME, 'submit'))
    address.add_address()
    print(address.get_name())

    # address.alter_address()
    # address.delete_address()/
